package com.seadragon.app.ksr.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.seadragon.app.ksr.dao.BusinessDao;
import com.seadragon.app.ksr.dao.OfficeDao;
import com.seadragon.app.ksr.model.Business;
import com.seadragon.app.ksr.service.AddressService;
import com.seadragon.app.ksr.service.BusinessService;
import com.seadragon.app.ksr.service.GeocodeService;
import com.seadragon.app.ksr.service.SearchService;
import com.seadragon.app.ksr.util.IndexManager;

@Service("businessService")
@Transactional
public class BusinessServiceImpl implements BusinessService {
	private Logger logger = LoggerFactory.getLogger(BusinessServiceImpl.class);
	@Autowired
	private BusinessDao businessDao;
	
	@Autowired
	private OfficeDao officeDao;
	
	@Autowired
	private SearchService searchService;

	@Autowired
	private AddressService addressService;

	@Autowired
	private GeocodeService geocodeService;

	@Autowired
	private IndexManager indexManager;
	
	@Override
	public Business update(Business business) {
		Business business2 = businessDao.findById(business.getId());
		if(business.getPassword() == null || business.getPassword().length() == 0){
			business.setPassword(business2.getPassword());
		}else{
			business2.setPassword(business.getPassword());
		}
		business2.setBusinessName(business.getBusinessName());
		business2.setDescription(business.getDescription());
		business2.setEmailAddress(business.getEmailAddress());
		business2.setSpecialty(business.getSpecialty());
		business2.setPhoneNumber(business.getPhoneNumber());
		business2.setWebSite(business.getWebSite());
		business2.setDescription(business.getDescription());
		
		business2.setCategories(business.getCategories());
		Business newBusiness = businessDao.update(business2);

		return newBusiness;
	}

//	private int calculatePriceRange(Office office){
//		Set<Category> categories = office.getBusiness().getCategories();
//		int minPriceRange = 4;
//		int priceRange;
//		for(Category category : categories){
//			CategoryPriceCategorization categorization = category.getPriceCategorizations().iterator().next();
//			if(office.getPrice() <= categorization.getOneDollarPrice()){
//				priceRange = 1;
//			}else if(office.getPrice() <= categorization.getTwoDollarPrice()){
//				priceRange = 2;
//			}else if(office.getPrice() <= categorization.getThreeDollarPrice()){
//				priceRange = 3;
//			}else{
//				priceRange = 4;
//			}
//			if(priceRange < minPriceRange) minPriceRange = priceRange;
//		}
//		return minPriceRange;
//	}
	
	@Override
	public void save(Business business) {
		businessDao.save(business);
	}

	@Override
	@Transactional(readOnly=true)
	public Business findById(int id) {
		return businessDao.findById(id);
	}

	@Override
	@Transactional(readOnly=true)
	public Business findByEmail(String email) {
		return businessDao.findByEmail(email);
	}

	@Override
	public List<Business> findAll(int offset, int pageSize) {
		return businessDao.findAll(offset, pageSize);
	}

}
